﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TLWindowsEditorWinFormsDemo
{
    /// <summary>
    /// 窗体帮助类
    /// by IT_MAOFY 2024/01/11
    /// </summary>
    public static class ControlHelper
    {
        /// <summary>
        /// 注册窗体文本框的回车事件发送Tab，切换控件
        /// </summary>
        /// <param name="frm"></param>
        public static void RegisterEnterSendTab(this Form frm)
        {
            frm.Load += (sender1, e1) =>
            {
                var controlList = GetControlList(frm);
                foreach (Control ct in controlList)
                {
                    TextBox tx = ct as TextBox;
                    if (tx == null)
                    {
                        continue;
                    }
                    if (tx.Enabled == false || tx.ReadOnly == true)
                    {
                        continue;
                    }
          
                    tx.KeyDown += (sender, e) =>
                    {
                        if (e.KeyCode != Keys.Enter)
                        {
                            return;
                        }
                        SendKeys.Send("{Tab}");
                    };
                }
            };
        }

        /// <summary>
        /// 获取控件列表
        /// </summary>
        /// <param name="frm"></param>
        public static List<Control> GetControlList(this Form frm)
        {
            List<Control> list = new List<Control>();
            list.Add(frm);
            foreach (Control control in frm.Controls)
            {
                list.Add(control);
                GetControlListCore(control, ref list);
            }
            return list;
        }

        /// <summary>
        /// 获取控件列表核心方法（递归获取控件）
        /// </summary>
        /// <param name="parentControl"></param>
        /// <param name="list"></param>
        private static void GetControlListCore(Control parentControl, ref List<Control> list)
        {
            foreach (Control control in parentControl.Controls)
            {
                list.Add(control);
                GetControlListCore(control, ref list);
            }
        }

        /// <summary>
        /// 获取所有的控件
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static List<Control> GetControlList(this Control parent) 
        {
            List<Control> list = new List<Control>();
            foreach (Control control in parent.Controls)
            {
                GetControlListCore(control, ref list);
            }
            return list;
        }

        public static List<ToolStripItem> GetControlList(this ToolStrip ts) 
        {
            List<ToolStripItem> list = new List<ToolStripItem>();
            //菜单
            foreach (ToolStripItem item in ts.Items)
            {
                GetControlListCore(item, ref list);
            }
            return list;
        }

        /// <summary>
        /// 获取控件列表核心方法（递归获取控件）
        /// </summary>
        /// <param name="parentControl"></param>
        /// <param name="list"></param>
        private static void GetControlListCore(ToolStripItem parentControl, ref List<ToolStripItem> list)
        {
            //if (parentControl is ToolStripSplitButton)
            //{
            //    list.Add(parentControl);
            //    return;
            //}

            if (parentControl is ToolStripSplitButton)
            {
                list.Add(parentControl);
                foreach (ToolStripItem child in (parentControl as ToolStripSplitButton).DropDownItems)
                {
                    GetControlListCore(child, ref list);
                }
            }
            else 
            {
                list.Add(parentControl);
            }
        }


        public static List<ToolStripMenuItem> GetControlList(this MenuStrip ms)
        {
            List<ToolStripMenuItem> list = new List<ToolStripMenuItem>();
            foreach (ToolStripMenuItem item in ms.Items)
            {
                list.Add(item);
                GetControlListCore(item, ref list);
            }
            return list;
        }

        /// <summary>
        /// 获取控件列表核心方法（递归获取控件）
        /// </summary>
        /// <param name="parentControl"></param>
        /// <param name="list"></param>
        private static void GetControlListCore(ToolStripMenuItem parentControl, ref List<ToolStripMenuItem> list)
        {
            foreach (ToolStripItem item in parentControl.DropDownItems) 
            {
                if (item is ToolStripMenuItem) 
                {
                    list.Add(item as ToolStripMenuItem);
                    GetControlListCore(item as ToolStripMenuItem, ref list);
                }
            }
        }


        public static List<ToolStripMenuItem> GetControlList(this ContextMenuStrip ms)
        {
            List<ToolStripMenuItem> list = new List<ToolStripMenuItem>();
            foreach (ToolStripItem item in ms.Items)
            {
                if (item is ToolStripMenuItem) 
                {
                    list.Add(item as ToolStripMenuItem);
                    GetControlListCore(item as ToolStripMenuItem, ref list);
                }
               
            }
            return list;
        }


        public static List<ToolStripItem> GetControlList(this StatusStrip ms)
        {
            List<ToolStripItem> list = new List<ToolStripItem>();
            foreach (ToolStripItem item in ms.Items)
            {
                list.Add(item);
            }
            return list;
        }
    }
}
